package com.yanqu.road.server.channelserver.timer.server;

import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.cross3.server.CrossChannelServerProto;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.server.channelserver.logic.ChannelMessageHelper;
import com.yanqu.road.server.channelserver.manager.server.ChannelRegisterServerMgr;
import com.yanqu.road.server.channelserver.manager.server.ChannelXServerNameMgr;
import com.yanqu.road.utils.executor.GeneralTask;

public class ChannelXServerNameTask extends GeneralTask {
    @Override
    public void doRun() {

        CrossChannelServerProto.ServerNameListMsg.Builder builder = ChannelXServerNameMgr.getServerNameMap().getAllServerNameMsg();

        // 每个渠道，同步非自己渠道的数据
        for (int channelId : ChannelRegisterServerMgr.getChannelIdList()) {
            CrossChannelServerProto.ServerNameListMsg.Builder tmpBuilder = CrossChannelServerProto.ServerNameListMsg.newBuilder();
            for (CrossChannelServerProto.ServerNameMsg serverNameMsg : builder.getNameListList()) {
                if (serverNameMsg.getChannelId() != channelId) {
                    tmpBuilder.addNameList(serverNameMsg);
                }
            }

            // 发送
            YanQuMessage message = YanQuMessageUtils.buildMessage(Cross3Protocol.C3_SYNC_SERVER_NAME_FROM_CHANNEL, tmpBuilder);
            ChannelMessageHelper.sendToChannel(message, channelId);
        }
    }
}
